/**
 * FileName: WechatIndexController
 * Author:   Leopold
 * Date:     2018/9/4 11:16
 * Description: 微信入口
 * History:
 */
package cn.mcsys.platform.core.web;

import cn.mcsys.myadmin.common.utils.DateUtils;
import cn.mcsys.myadmin.common.web.JsonResult;
import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.mobile.entity.AirRealtimeUserCount;
import cn.mcsys.myadmin.modules.mobile.entity.AirRealtimeUserOrigin;
import cn.mcsys.myadmin.modules.mobile.entity.AirUserFlowCount;
import cn.mcsys.platform.core.service.PlatformService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class AirPassengerFlowController {

    private static Logger logger = LoggerFactory.getLogger(AirPassengerFlowController.class);

    @Autowired
    private BackService backService;

    @Autowired
    private PlatformService platformService;

    @RequestMapping("airPassengerFlow")
    public String airPassengerFlow(Model model) {
        System.out.println("go to airPassengerFlow!getting data");
        return "airPassengerFlow";
    }

    @GetMapping("getUserCounts")
    @ResponseBody
    public JsonResult getUserCountsToday() {
        List<AirRealtimeUserCount> countsToday = backService.getUserCountsToday();
        Map<String, AirRealtimeUserCount> result = new HashMap<>();
        for (AirRealtimeUserCount userCount : countsToday) {
            String time = DateUtils.formatDate(userCount.getStartDateTime(), "HH:mm:ss");
//            if (DateUtils.isInDate(DateUtils.getTodayTime(time), "07:00:00", "20:00:00")) {
//                if (result.get(time) != null) {
//                    AirRealtimeUserCount airRealtimeUserCount = result.get(time);
//                    airRealtimeUserCount.setUserCount(airRealtimeUserCount.getUserCount() + userCount.getUserCount());
//                    result.put(time, airRealtimeUserCount);
//                } else {
//                if(time.split(":")[1].equals("00")||time.split(":")[1].equals("30")) {
                    if(result.get(time)==null) {
                        result.put(time, userCount);
                    }else {
                        AirRealtimeUserCount count = result.get(time);
                        count.setUserCount(count.getUserCount()+userCount.getUserCount());
                        result.put(time,count);
                    }
//                }
//                }
//            }
        }

        Comparator<Map.Entry<String, AirRealtimeUserCount>> valueComparator = new Comparator<Map.Entry<String, AirRealtimeUserCount>>() {
            @Override
            public int compare(Map.Entry<String, AirRealtimeUserCount> o1,
                               Map.Entry<String, AirRealtimeUserCount> o2) {
                return DateUtils.parseDate(o2.getKey()).before(DateUtils.parseDate(o1.getKey())) ? 1 : -1;
            }
        };
        // map转换成list进行排序
        List<Map.Entry<String, AirRealtimeUserCount>> list = new ArrayList<Map.Entry<String, AirRealtimeUserCount>>(result.entrySet());
        // 排序
        Collections.sort(list, valueComparator);

        return JsonResult.success(list);
    }

    @GetMapping("getUserFlowCounts")
    @ResponseBody
    public JsonResult getUserFlowCountsToday() {
        List<AirUserFlowCount> flowCounts = backService.getUserFlowCountsToday();
        Map<String, AirUserFlowCount> result = new HashMap<>();
        for (AirUserFlowCount flowCount : flowCounts) {
            String time = DateUtils.formatDate(flowCount.getStartDateTime(), "HH:mm:ss");
//            if (DateUtils.isInDate(DateUtils.getTodayTime(time), "06:30:00", "20:00:00")) {
                if (result.get(time) != null) {
                    AirUserFlowCount airUserFlowCount = result.get(time);
                    airUserFlowCount.setUserInflowCount(airUserFlowCount.getUserInflowCount() + flowCount.getUserInflowCount());
                    airUserFlowCount.setUserOutflowCount(airUserFlowCount.getUserOutflowCount() + flowCount.getUserOutflowCount());
                    result.put(time, airUserFlowCount);
                } else {
                    result.put(time, flowCount);
                }
            }
//        }
        return JsonResult.success(result);
    }

    @GetMapping("getHeatData")
    @ResponseBody
    public JsonResult getHeatData() {
        List<AirRealtimeUserCount> countsToday = backService.getUserCountsLeast();
        Map<String, AirRealtimeUserCount> result = new HashMap<>();
        for (AirRealtimeUserCount userCount : countsToday) {
            result.put(String.valueOf(userCount.getAreaId()),userCount);
        }
        return JsonResult.success(result);
    }

    @GetMapping("getDistributionData")
    @ResponseBody
    public JsonResult distributionData() {
        //todo 获取地区人员分布
        Map<String, AirRealtimeUserOrigin> result = new HashMap<>();
        List<AirRealtimeUserOrigin> userOriginLeast = backService.getUserOriginLeast();
        for (AirRealtimeUserOrigin origin : userOriginLeast) {
            if(result.get(origin.getOriginName())!=null){
                origin.setUserCount(origin.getUserCount()+result.get(origin.getOriginName()).getUserCount());
            }
            result.put(origin.getOriginName(),origin);
        }
        return JsonResult.success(result);
    }
}
